package com.winit.openapi.mms.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.mms.spi.command.CustomsTaxCalculateCommand;
import com.winit.mms.spi.customs.CustomsTaxCalculateService;
import com.winit.mms.spi.vo.CustomsTaxVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * 
 *  税金计算controller
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * hao.gong 	1.0  		2015年5月19日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/mms/calculator")
public class TaxCalculatorController extends BaseController {
    //商品税金  保留两位小数
    public static final int        MERCHANDISE_TAX_SCALE                       = 2;
    @Resource
    CustomsTaxCalculateService customsTaxCalculateService;
    /**
     * 根据商品code和数量计算商品税金
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/tax")
    public String queryTaxByCodeAndCount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        String code = null;
        String taxType = null;
        Integer count = null;
        String currency = "USD";//默认币种为USD
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 校验参数
            ValidateUtil.validNotNull(json, "merchandiseCode");
            ValidateUtil.validNotNull(json, "taxType");
            ValidateUtil.validInteger(json, "count", true);
            code = json.getString("merchandiseCode").trim();
            count = json.getInteger("count");
            currency = StringUtils.isNotBlank(json.getString("currency").trim()) ? json.getString("currency").trim() : currency;
            taxType = json.getString("taxType").trim();
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 组装command
        CustomsTaxCalculateCommand command = new CustomsTaxCalculateCommand();
        command.setCtx(CommandContext.getContext());
        command.setPartnerId(Integer.valueOf(CommandContext.getContext().getUserId()));
        command.setMerchandiseCode(code);
        command.setTaxCurrencyCode(currency);
        command.setTaxType(taxType);

        // 调用接口查询商品信息
        CustomsTaxVo vo = customsTaxCalculateService.calculate(command);
        Double tax = vo.getTax();
        BigDecimal taxRes = BigDecimal.valueOf(tax)
            .multiply(new BigDecimal(count))
            .setScale(MERCHANDISE_TAX_SCALE, BigDecimal.ROUND_HALF_UP);
        
        Map<String, BigDecimal> data = new HashMap<String, BigDecimal>();
        data.put("tax", taxRes);
        responseMsg.setData(data);
        return SUCCESS;
    }
}
